﻿using System;
using System.Collections.Generic;
using System.Linq;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace DL.CfgData
{
    /// <summary>
    /// 配置数据集.
    /// 所有配置数据的访问接口由此类提供.
    /// </summary>
    public static class CfgDataCollection
    {
        private static readonly Dictionary<Type, TextCfgData> textCfgDatas = new();

        // private static readonly Dictionary<Type, IScriptableCfgData> _scriptableCfgDatas =
        //     new Dictionary<Type, IScriptableCfgData>(6);


        #region 基本方法

        private static T GetTextCfg<T>() where T : TextCfgData
        {
            var typ = typeof(T);
            if (!textCfgDatas.ContainsKey(typ) || textCfgDatas[typ] == null)
            {
                textCfgDatas[typ] = InstanceCfg<T>();
                //做了资源加载
                textCfgDatas[typ].LoadConfig(ConfigDataHelper.ReadFileContent(textCfgDatas[typ].ConfigFileName));
            }

            return (T)textCfgDatas[typ];
        }

        // private static T GetScriptableCfg<T>() where T : class, IScriptableCfgData
        // {
        //     var typ = typeof(T);
        //     if (!_scriptableCfgDatas.ContainsKey(typ) || _scriptableCfgDatas[typ] == null)
        //     {
        //         _scriptableCfgDatas[typ] = InstanceCfg<T>();
        //         var scriptableObject = Loader.ScriptableConfigLoader<ScriptableObject>(_scriptableCfgDatas[typ].ConfigFileName);
        //         _scriptableCfgDatas[typ].LoadConfig(scriptableObject);
        //     }
        //
        //     return (T)_scriptableCfgDatas[typ];
        // }


        private static T InstanceCfg<T>()
        {
            return Activator.CreateInstance<T>();
        }

        public static void Unload<T>() where T : TextCfgData
        {
            var typ = typeof(T);
            if (textCfgDatas.ContainsKey(typ))
            {
                textCfgDatas.Remove(typ);
            }
        }

        public static void UnloadAll()
        {
            textCfgDatas.Clear();
        }

        public static void ReloadAll()
        {
            var arr = textCfgDatas.Keys.ToArray();
            foreach (var typ in arr)
            {
                textCfgDatas[typ] = (TextCfgData)Activator.CreateInstance(typ);
                textCfgDatas[typ].LoadConfig(ConfigDataHelper.ReadFileContent(textCfgDatas[typ].ConfigFileName));
            }
        }

        /// <summary>
        /// 卸载指定的配置文件
        /// </summary>
        /// <param name="fileNames"></param>
        public static void UnloadSelectCfg(IList<string> fileNames)
        {
            if (fileNames != null && fileNames.Count > 0)
            {
                var types = textCfgDatas.Keys.ToArray();
                foreach (var t in types)
                {
                    if (textCfgDatas[t] != null)
                    {
                        var textCfgData = textCfgDatas[t];
                        var isRelativeToFiles = false;
                        foreach (var fileName in fileNames)
                        {
                            if (textCfgData.IsRelativeToFile(fileName))
                            {
                                isRelativeToFiles = true;
                                break;
                            }
                        }

                        if (isRelativeToFiles)
                        {
                            textCfgDatas[t] = null;
                        }
                    }
                }
            }
        }

        #endregion

        public static ActivityCfgData ActivityCfgData => GetTextCfg<ActivityCfgData>();
        public static TaskCfgData TaskCfgData => GetTextCfg<TaskCfgData>();
    }
    
}

